//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;

namespace Microsoft.Practices.Mobile.ObjectBuilder
{
	/// <summary>
	/// Represents the information necessary for registration of a builder policy. A policy is
	/// registered by the interface policy type, the type the policy applies to, and the ID
	/// the policy applies to.
	/// </summary>
	public struct BuilderPolicyKey
	{
		private Type PolicyType;
		private Type BuildType;
		private string BuildID;

		/// <summary>
		/// Initializes a new instance of the <see cref="BuilderPolicyKey"/> struct using the
		/// provided policy type, application type, and application ID.
		/// </summary>
		/// <param name="policyType">The policy interface type.</param>
		/// <param name="typePolicyAppliesTo">The type the policy applies to.</param>
		/// <param name="idPolicyAppliesTo">The ID the policy applies to.</param>
		public BuilderPolicyKey(Type policyType, Type typePolicyAppliesTo, string idPolicyAppliesTo)
		{
			PolicyType = policyType;
			BuildType = typePolicyAppliesTo;
			BuildID = idPolicyAppliesTo;
		}

		/// <summary>
		/// Checks for equality between two keys.
		/// </summary>
		public static bool operator ==(BuilderPolicyKey x, BuilderPolicyKey y)
		{
			return x.Equals(y);
		}

		/// <summary>
		/// Checks for inequality between two keys.
		/// </summary>
		public static bool operator !=(BuilderPolicyKey x, BuilderPolicyKey y)
		{
			return !x.Equals(y);
		}

		/// <summary>
		/// Compares the current instance with the given object for equality.
		/// </summary>
		public override bool Equals(object obj)
		{
			//       
			// See the full list of guidelines at
			//   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconequals.asp    
			// and also the guidance for operator== at
			//   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconimplementingequalsoperator.asp
			//
			if (obj == null || GetType() != obj.GetType())
			{
				return false;
			}

			BuilderPolicyKey other = (BuilderPolicyKey)obj;
			return other.BuildID == this.BuildID &&
				other.BuildType == this.BuildType &&
				other.PolicyType == this.PolicyType;
		}

		/// <summary>
		/// Returns a hash code for the key.
		/// </summary>
		public override int GetHashCode()
		{
			int hash = 0;
			if (PolicyType != null) hash = PolicyType.GetHashCode();
			if (BuildType != null) hash ^= BuildType.GetHashCode();
			if (BuildID != null) hash ^= BuildID.GetHashCode();

			return hash;
		}
	}
}